<?php
			
	
	interface category_info
	{
		function checkExistsID($recordID);
		function checkExistsCatCode($CatCode);
		function checkExistsCat($CatID,$CatCode);
		function updateValue($CatID,$catName,$hasBarcodeQRCode,$ModifiedBy);	
		function insertValue($catCode,$catName,$hasBarQRCode,$ModifiedBy);
		function deleteValue($oldCatID);
		function getCategoriesList();
		function getCATList($barcode);
		function getCategoriesListOrderBy($orderBy);
		function hasBarcode($catID);
		function getNewCatCode();
		//Validate Cat ID, Cat Code
		function validateCatID($CateID);
		function validateCatCode($CateCode);
		function getNameCATList($CATCode);
		
	}
	
	class category implements category_info
	{
		function category()
		{
			@session_start(); 
			require_once('../../lib/'.'BaseDir.inc.htm');
			require_once(EIU_LIB.'DBCommon.class.htm');					
		}
		
		function hasBarcode($catCode)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
									
			$q = "
				SELECT hasBarQRCode
				FROM category_tbl 
				WHERE `isDeleted`=0 AND `CATCode` = '".$catCode."'	
				GROUP BY `CATCode`
			";
			
			//echo $q;
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;		
		}
		
		function validateCatCode($CateCode){
			//Cat Code phải là chữ hoa có 3 kí tự
			if (preg_match("/[^A-Z]{3}/", $CateCode, $matches)) {
				return false;
			}else return true;			
		}
		
		function validateCatID($CateID){
			//Cat ID là số nguyên
			/*
			if(preg_match("/[^0-9]/", $CateID, $matches)){				
				return false;
			}else return true;
			*/
			if(is_numeric($CateID)) return true;
			else return false;
		}
		
		function getCategoriesList(){

			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
									
			$q = "
				SELECT CATID,CATCode,CATName,hasBarQRCode 
				FROM category_tbl 
				WHERE `isDeleted`=0
				ORDER BY `CATName` ASC
			";
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;			
			
		}
		function getCATList($barcode)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
									
			$q = "
				SELECT CATID,CATCode,CATName,hasBarQRCode 
				FROM category_tbl 
				WHERE `isDeleted`=0 and `hasBarQRCode`=".$barcode."
				ORDER BY `CATName` ASC
			";
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;	
		}
		function getCategoriesListOrderBy($orderBy)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
									
			$q = "
				SELECT CATID,CATCode,CATName,hasBarQRCode 
				FROM category_tbl 
				WHERE `isDeleted`=0 
				ORDER BY `".$orderBy."` ASC
			";
			
			//echo $q;
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;	
		}
		
		function getNewCatCode()
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
									
			$q = "
				SELECT LPAD((MAX(CATCode) + 1),3,'0') AS NewCatCode
				FROM category_tbl 
				WHERE `isDeleted`=0					
			";
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs[0]->{"NewCatCode"};
			else return false;	
		}
		
		function checkExistsCat($CatID,$CatCode){
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			
			$q_select_catid = "SELECT COUNT(CATCode) as count FROM `category_tbl` WHERE `CATID`='".$CatID."' AND `CATCode`='".$CatCode."'";
			//echo $q_select_catid;
			$rs_select = $oDB->stl_db_select($q_select_catid, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs_select[0]->count<=0){
				return false;
			}else{
				return true;
			}
			
		}
		
		function checkExistsID($recordID){
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			
			$q_select_catid = "SELECT COUNT(*) as count FROM `category_tbl` WHERE `CatID`='".$recordID."'";
			$rs_select = $oDB->stl_db_select($q_select_catid, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs_select[0]->count<=0){
				return false;
			}else{
				return true;
			}
			
		}
		
		function checkExistsCatCode($CatCode){
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			
			$q_select_catid = "SELECT COUNT(CATCode) as count FROM `category_tbl` WHERE `CATCode`='".$CatCode."'";
			$rs_select = $oDB->stl_db_select($q_select_catid, $conn);
			
			
			$oDB->stl_closeConn($conn);
			
			if($rs_select[0]->count<=0){
				return false;
			}else{
				return true;
			}
		}
		
		function updateValue($CatID,$catName,$hasBarcodeQRCode,$ModifiedBy){
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
		
			$q_update = "UPDATE `category_tbl` SET `CATName` = '".$catName."',`hasBarQRCode` = '".$hasBarcodeQRCode."',								 
								`LastUpdate`= CURRENT_TIMESTAMP,`ModifiedBy`= '".$ModifiedBy."' 
						WHERE CATCode != '000' AND `category_tbl`.`CATID` =".$CatID
						;
			
			//echo $q_update;
			$rs_update = $oDB->stl_db_change($q_update, $conn);
			
			$oDB->stl_closeConn($conn);
		}
		
		//Hàm insert dữ liệu một row mới
		function insertValue($catCode,$catName,$hasBarQRCode,$ModifiedBy){
			
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();

			$q_insert = "
				INSERT INTO `category_tbl` (`CATCode`, `CATName`,`hasBarQRCode`, `ModifiedBy`, `isDeleted`, `CreateDate`, `LastUpdate`) 
				VALUES ('".$catCode."', '".$catName."','".$hasBarQRCode."', '".$ModifiedBy."',0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
			";
			
			//echo $q_insert;
			$rs_insert = $oDB->stl_db_select($q_insert, $conn);
		
			$oDB->stl_closeConn($conn);
		}
		
		function deleteValue($oldCatID){
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			$today = date("Y-m-d"); 
			
			//Chuyển isDeleted = 1 trong category_tbl
			$q_update = "Update `category_tbl` set `isDeleted`=1 where CATCode != '000' AND `CATID`=".$oldCatID;
			
			$rs_update = $oDB->stl_db_change($q_update, $conn);
			
			$oDB->stl_closeConn($conn);			
			
		}
		function getNameCATList($CATCode){

			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
									
			$q = "SELECT distinct(`AssName`) FROM `asset_tbl` 
			WHERE `CATCode`='".$CATCode."' and `isDeleted`=0";
			//echo $q;
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;		
		}	
	}
	
	
?>